<?php

declare(strict_types=1);

namespace App\Traits\Model\Users;

use App\Model\Shop\Shop;
use App\Scopes\Users\User as UserScope;
use Hyperf\Database\Model\Relations\BelongsTo;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;
use App\Model\Users\UserOauth as UserOauthModel;

trait User
{
    /**
     * The "booting" method of the model.
     */
    protected function boot():void
    {
        parent::boot();
        static::addGlobalScope(new UserScope);
    }

    /**
     * 店铺
     * @param $value
     */
    public function setShopIdAttribute($value)
    {
        if (!$value) {
            $value = match (getApp()) {
                'api' => getShop('id')
            };
        }

        $this->attributes['shop_id'] = $value;
    }

    /**
     * 关联店铺
     * @return BelongsTo
     */
    public function shop():BelongsTo
    {
        return $this->belongsTo(Shop::class);
    }

    /**
     * 授权
     * @return HasOne
     */
    public function oauth():HasMany
    {
        return $this->hasMany(UserOauthModel::class);
    }
}
